<template>
  <div class="el-upload">
    <el-form>
      <el-upload
          :limit="1"
          action=""
          drag
          accept=".pdf, .doc, .docx, .xls, .xlsx"
          :http-request="uploadFile"
          :auto-upload="false"
          :file-list="fileList"
          ref="uploadFileXls"
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
<!--        <div class="el-upload__tip" slot="tip">只能上传xls/xlsx文件，且不超过2000kb</div>-->
        <!--        <img src="https://img-home.csdnimg.cn/images/20201124032511.png" width="146" height="146">-->
      </el-upload>
<!--      <el-form-item>-->
<!--        <el-button size="small" type="primary" style="margin-top: 20px;" @click="submitUpload">点击上传</el-button>-->
<!--      </el-form-item>-->
<!--      <el-dialog :visible.sync="dialogVisible" append-to-body>-->
<!--        <img width="100%" fit="contain" :src="dialogImageUrl" alt="">-->
<!--      </el-dialog>-->
    </el-form>
  </div>
</template>
<script>
// import { uploadImg, delUpIamge } from '@/api/publicSeries'
export default {
  props: {
    limit: Number,
    fileList: Array
  },
  data () {
    return {
      imageUrl: '',
      dialogImageUrl: '',
      dialogVisible: false
    }
  },
  methods: {
    submitUpload () { this.$refs.uploadFileXls.submit() },
    uploadFile (e) {
      console.log(this.dialogData, 'upload-file')
      const file = e.file
      const size = file.size / 1024 / 1024 / 2
      console.log(file)
      // if (!(file.type === 'image/png' || file.type === 'image/gif' || file.type === 'image/jpg' || file.type === 'image/jpeg')) {
      if (size > 2) {
        this.$notify.warning({
          title: '警告',
          message: '文件大小必须小于2M'
        })
      } else {
        const params = new FormData()
        params.append('file', file)
        console.log(params)
        this.$api.uploadFilesExport(params).then(res => {
          if (res.code === 0) {
            this.$message.success('上传成功')
            this.downloadlink(res.data)
            this.$emit('dialogVisible', this.dialogVisible)
          } else {
            this.$message.error('上传失败')
          }
        })
      }
    },
    downloadlink (url) {
      const protocol = location.protocol
      let resultUrl = url
      if (protocol === 'https:') {
        resultUrl = url.replace(`http:`, `${protocol}`)
      }
      const iframe = document.createElement('iframe')
      iframe.style.display = 'none'
      iframe.src = resultUrl
      document.body.appendChild(iframe)
    }
  }
}
</script>

